/*
 * @Author: songtao
 * @Date: 2021-11-22 15:35:50
 * @LastEditTime: 2021-11-22 19:15:24
 * @FilePath: \vue-cars\vue-admin\src\router\permission\index.js
 */

import router from "..";
import store from "../../store"
import { getToken , removeToken , removeUserName } from "../../utils/app";

const whiteRouter = ['/login'];

router.beforeEach((to,from,next) => {
    if(getToken()){
        // 用户强制跳转到登录页：清除token
        if(to.path === '/login'){
            removeToken()
            removeUserName()
            store.commit("SET_TOKEN","")
            store.commit("SET_USERNAME","")
            next()
        }else{
            // 获取用户角色
            // 动态分配路由权限
            next()
        }
    }else{
        if(whiteRouter.indexOf(to.path) !== -1){
            next()
        }else{
            next('/login')
        }
    }
})